<?php

namespace App\Admin\User\Controllers;

use App\Admin\User\Models\User;
use App\Classlib\FormCheck;
use Illuminate\Http\Request;
use App\Admin\Controller;

class UserController extends Controller
{
    public function __construct(){
        $this->formCheck = new FormCheck();
        $this->user    = new User();
    }

    //添加用户信息
    public function create(Request $request){
        $checkName = $this->formCheck->isEmpty($request->name,Lang('User','username'));
        if(!$checkName->code){
            return result($checkName->msg);
        }
        $checkPhone = $this->formCheck->isEmpty($request->mobile,"用户电话");
        if(!$checkPhone->code){
            return result($checkPhone->msg);
        }
        $checkmobile = $this->formCheck->mobile($request->mobile);
        if(!$checkmobile->code){
            return result($checkmobile->msg);
        }
        $checkIsExtMobile = $this->user->checkFieldExist("mobile",$request->mobile);
        if($checkIsExtMobile->code){
            return result("手机号码已注册，请更换！");
        }
        $checkPassword = $this->formCheck->isEmpty($request->password,"用户密码");
        if(!$checkPassword->code){
            return result($checkPassword->msg);
        }
        $checkPasswordFloat = $this->formCheck->password($request->mobile,1);
        if(!$checkPasswordFloat->code){
            return result($checkPasswordFloat->msg);
        }
        $addData = array(
            "name"           => $request->name,
            "mobile"         => $request->mobile,
            "password"       => bcrypt($request->password),
            "head_image_url" => "users/default.png",
            "parent_user_id" => 0,
        );
        $result = $this->user->registerUser($addData);
        if($result){
            $this->actionLog("添加用户");
            return result("添加用户成功！",1);
        }else{
            return result("添加用户失败，稍后重试！",0);
        }

    }

    //提交编辑内容
    public function update(Request $request){
        $checkName = $this->formCheck->isEmpty($request->name,"门店名称");
        if(!$checkName->code){
            return result($checkName->msg);
        }
        $checkPhone = $this->formCheck->isEmpty($request->phone,"门店电话");
        if(!$checkPhone->code){
            return result($checkPhone->msg);
        }
        $checkResponsePerson = $this->formCheck->isEmpty($request->response_person,"负责人");
        if(!$checkResponsePerson->code){
            return result($checkResponsePerson->msg);
        }
        $checkResponsePhone = $this->formCheck->isEmpty($request->response_phone,"联系电话");
        if(!$checkResponsePhone->code){
            return result($checkResponsePhone->msg);
        }
        $result = $this->store->StoreUpdate($request);
        return result($result->msg,$result->code);
    }

    public function updateUserState(Request $request){
        if(!actionIsView("blacklist_users")){
            return redirect(adminurl("/unauthorized"));
        }
        $checkUserId = $this->formCheck->isEmpty($request->id,"用户ID");
        if(!$checkUserId->code){
            return result("用户ID不能为空！");
        }
        if(!in_array($request->state,array(0,1))){
            return result("更新状态不存在！");
        }
        $result = $this->user->updateUserState($request);
        if($result->code==1){
            if($request->state==1){
                $log_msg = '移出用户黑名单';
            }else{
                $log_msg = '加入用户黑名单';
            }
            $this->actionLog($log_msg);
        }
        return result($result->msg,$result->code);
    }

    //数据用户列表
    public function lists(Request $request){
        $dataContent = $this->user->getUserLists($request);
        /*
        if (count($dataContent['lists'])) {
            foreach ($dataContent['lists'] as $data) {
                $data->payTimes = $this->user->getPayTimes($data->uid);
                //获取用户等级
                $data->user_level_name = $this->user->getUserLevelName($data->level_id);
            }
        } else {
            $dataContent['lists'] = array();
        }*/
        return result("请求成功",1,$dataContent);
    }
}